package com.chwn.curiousdiary.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.bean.StoryListBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.harulib.adapter.ViewHolder;
import com.chwn.harulib.adapter.base.MultiBaseAdapter;

import java.util.List;


/**
 * Created by Myshikin on 2017/6/18.
 */

public class StoryPackageAdapter extends MultiBaseAdapter<StoryListBean> {

    private int[] fancyIds = {R.drawable.bg_fancy_zero, R.drawable.bg_fancy_one, R.drawable.bg_fancy_two, R.drawable.bg_fancy_three
            , R.drawable.bg_fancy_four, R.drawable.bg_fancy_one_end, R.drawable.bg_fancy_two_end, R.drawable.bg_fancy_three_end
            , R.drawable.bg_fancy_four_end, R.drawable.bg_fancy_one_other};
    private int[] buildIds = {R.drawable.bg_build_zero, R.drawable.bg_build_one, R.drawable.bg_build_two, R.drawable.bg_build_three
            , R.drawable.bg_build_four, R.drawable.bg_build_one_other, R.drawable.bg_build_two_end, R.drawable.bg_build_three_end
            , R.drawable.bg_build_four_end, R.drawable.bg_build_one_other, R.drawable.bg_build_two_other};

    private int[] lifeIds = {R.drawable.bg_life_zero, R.drawable.bg_life_one, R.drawable.bg_life_two, R.drawable.bg_life_three
            , R.drawable.bg_life_four, R.drawable.bg_life_one_end, R.drawable.bg_life_two_end, R.drawable.bg_life_three_end
            , R.drawable.bg_life_four_end, R.drawable.bg_life_one_other, R.drawable.bg_life_two_other};

    private int[] newIds = {R.drawable.bg_new_zero, R.drawable.new_one, R.drawable.new_two, R.drawable.new_three
            , R.drawable.new_four, R.drawable.bg_new_one_end, R.drawable.bg_new_two_end, R.drawable.bg_new_three_end
            , R.drawable.bg_new_four_end, R.drawable.new_one};

    private int[] animalIds = {R.drawable.bg_animal_zero, R.drawable.bg_animal_one, R.drawable.bg_animal_two, R.drawable.bg_animal_three
            , R.drawable.bg_animal_four, R.drawable.bg_animal_one_end, R.drawable.bg_animal_two_end, R.drawable.bg_animal_three_end
            , R.drawable.bg_animal_four_end, R.drawable.bg_animal_one_other};

    private int[] currentUse;
    private int   mTag;

    public StoryPackageAdapter(Context context, List<StoryListBean> datas, boolean isOpenLoadMore, int jumpTag) {
        super(context, datas, isOpenLoadMore, true);

        mTag = jumpTag;
        switch (jumpTag) {
            case Constant.ADVENTURE_TAG:
            case Constant.FANCY_TAG:
                currentUse = fancyIds;
                break;
            case Constant.NEW_TAG:
                currentUse = newIds;
                break;
            case Constant.BUILD_TAG:
                currentUse = buildIds;
                break;
            case Constant.LIFE_TAG:
                currentUse = lifeIds;
                break;
            case Constant.CHINABOOK_TAG:
            case Constant.ANIMAL_TAG:
                currentUse = animalIds;
                break;
        }
    }

    @Override
    protected void convert(final ViewHolder holder, final StoryListBean data, final int viewType, final int position) {
        ImageView lineView = holder.getView(R.id.iv_round_line);

        ImageView mIVPlay = holder.getView(R.id.iv_play);
        if (data.getsParts() == 2) { //故事包
            holder.setText(R.id.tv_story_name, data.getStoryPackageTitle());
            mIVPlay.setImageResource(R.drawable.selector_package_play);
        } else {
            String storyTitle = data.getStoryTitle();
            if (storyTitle.contains(" ")) {
                String[] split = storyTitle.split(" ");
                storyTitle = split[0] + "\n";
                for (int i = 1; i < split.length; i++) {
                    storyTitle += split[i];
                }
            }
            holder.setText(R.id.tv_story_name, storyTitle);
            mIVPlay.setImageResource(R.drawable.selector_story_play);
        }

        mIVPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StoryPackageAdapter.this.mItemClickListener.onItemClick(holder, data, position > mDatas.size() - 1 ? mDatas.size() - 1 : position, viewType);
            }
        });

        switch (viewType) {
            case Constant.list_zero:
                lineView.setBackgroundResource(currentUse[0]);
                break;
            case Constant.list_second:
                if (mTag == Constant.NEW_TAG) {
                    lineView.setBackgroundResource(R.drawable.new_one);
                } else {
                    lineView.setBackgroundResource(currentUse[9]);
                }
                break;
            case Constant.list_one:
                lineView.setBackgroundResource(currentUse[1]);

                if (position > 8 && mTag == Constant.BUILD_TAG) {
                    lineView.setBackgroundResource(currentUse[1]); //之前為9
                } else {
                    lineView.setBackgroundResource(currentUse[1]);
                }

                if (mTag == Constant.NEW_TAG) {
                    if (position == mDatas.size() - 2 || position == mDatas.size() - 3) {
                        lineView.setBackgroundResource(R.drawable.new_one_last);
                    } else {
                        lineView.setBackgroundResource(R.drawable.new_one_middle);
                    }
                }

                break;
            case Constant.list_two:

                if (position > 4 && mTag == Constant.BUILD_TAG) {
                    lineView.setBackgroundResource(currentUse[10]);
                } else if (position > 4 && mTag == Constant.BUILD_TAG) {
                    lineView.setBackgroundResource(currentUse[2]);
                } else {
                    lineView.setBackgroundResource(currentUse[2]);
                }

                if (position > 4 && mTag == Constant.LIFE_TAG) {
                    lineView.setBackgroundResource(currentUse[10]);
                } else if (mTag == Constant.LIFE_TAG) {
                    lineView.setBackgroundResource(currentUse[2]);
                }

                if (mTag == Constant.NEW_TAG) {
                    if (position == mDatas.size() - 2) {
                        lineView.setBackgroundResource(R.drawable.new_two_last);
                    } else if (position == 3) {  // position 比较常量有误
                        lineView.setBackgroundResource(R.drawable.new_two);
                    } else {
                        lineView.setBackgroundResource(R.drawable.new_two_middle);
                    }
                }

                break;
            case Constant.list_three:


                if (mTag == Constant.NEW_TAG) {
                    if (position == 2) {  // position 比较常量有误
                        lineView.setBackgroundResource(R.drawable.new_three);
                    } else {
                        lineView.setBackgroundResource(R.drawable.new_three_middle);
                    }
                } else {
                    lineView.setBackgroundResource(currentUse[3]);
                }

                break;
            case Constant.list_four:

                if (mTag == Constant.NEW_TAG) {
                    if (position == mDatas.size() - 2 || position == mDatas.size() - 4 || position == mDatas.size() - 3) {
                        lineView.setBackgroundResource(R.drawable.new_four_middle);
                    } else {
                        lineView.setBackgroundResource(R.drawable.new_four);
                    }
                } else {
                    lineView.setBackgroundResource(currentUse[4]);
                }

                break;
            case Constant.list_four_end:
                lineView.setBackgroundResource(currentUse[8]);
                break;
            case Constant.list_one_end:
                lineView.setBackgroundResource(currentUse[5]);
                break;
            case Constant.list_two_end:
                lineView.setBackgroundResource(currentUse[6]);
                break;
            case Constant.list_three_end:
                lineView.setBackgroundResource(currentUse[7]);
                break;

        }


        // 4*n + 3 == mDatas.size()
        if (isNeedPlus1 && position == mDatas.size() - 1 && isThree) {
            holder.itemView.setVisibility(View.GONE);
        } else {
            holder.itemView.setVisibility(View.VISIBLE);
        }

        if (isNeedPlus1 && position == mDatas.size() && isThree) {
            if (mTag == Constant.NEW_TAG) {
                lineView.setBackgroundResource(R.drawable.bg_new_two_end);
            } else {
                lineView.setBackgroundResource(currentUse[6]);
            }
        }

        // 4*n + 4 == mDatas.size()
        if (isFour && position == mDatas.size() - 1) {

            if (mTag == Constant.NEW_TAG) {
                lineView.setBackgroundResource(R.drawable.new_two_last);
            } else {
                lineView.setBackgroundResource(currentUse[2]);
            }

        } else if (isFour && position == mDatas.size() - 2) {

            if (mTag == Constant.NEW_TAG) {
                lineView.setBackgroundResource(R.drawable.bg_new_three_end);
            } else {
                lineView.setBackgroundResource(currentUse[7]);
            }
        }
    }

    @Override
    protected int getItemLayoutId(int viewType) {
        switch (viewType) {
            case Constant.list_zero:
                return R.layout.item_list_zero;
            case Constant.list_second:
                return R.layout.item_list_second;
            case Constant.list_one:
                return R.layout.item_list_one;
            case Constant.list_two:
                return R.layout.item_list_two;
            case Constant.list_three:
                return R.layout.item_list_three;
            case Constant.list_four:
                return R.layout.item_list_four;
            case Constant.list_four_end:
                return R.layout.item_list_four_end;
            case Constant.list_one_end:
                return R.layout.item_list_one_end;
            case Constant.list_two_end:
                return R.layout.item_list_two_end;
            case Constant.list_three_end:
                return R.layout.item_list_three_end;
        }
        return R.layout.item_list_zero;
    }

    @Override
    protected int getViewType(int position, StoryListBean data) {

        if (position == 0 || position == 1) {
            return position == 0 ? 0 : 9;
        } else if (position == mDatas.size() - 1) {
            if (position % 4 == 0) {
                return 8;
            }
            return position % 4 + 4;
        } else {
            if (position % 4 == 0) {
                return 4;
            }
            return position % 4;
        }
    }
}
